package com.zff.rental.mapstruct.apartment;


import com.zff.rental.entity.apartment.RoomEntity;
import com.zff.rental.mapstruct.BaseMapstruct;
import com.zff.rental.po.apartment.RecommendApartmentPo;
import com.zff.rental.vo.apartment.room.*;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author HCY
 */
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface RoomMapstruct extends BaseMapstruct {
    RoomMapstruct INSTANCE = Mappers.getMapper(RoomMapstruct.class);

    RoomVo map(RoomEntity findResult);

    RoomEntity map(AddRoomParams params);

    @Mapping(target = "id", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    void merge(@MappingTarget RoomEntity target, ModifyRoomParams source);

    /**
     * 批量修改
     * 原价格为空时，才修改价格
     */
    default void mergeList(List<RoomEntity> target, BatchModifyRoomParams source) {
        for (RoomEntity roomEntity : target) {
            mergeIgnorePrice(roomEntity, source);
            if (roomEntity.getPrice() == null && source.getPrice() != null) {
                roomEntity.setPrice(source.getPrice());
            }
            if (!StringUtils.hasText(roomEntity.getHouseType()) && StringUtils.hasText(source.getHouseType())) {
                roomEntity.setHouseType(source.getHouseType());
            }
            if (roomEntity.getRentalStatus() == null && source.getRentalStatus() != null) {
                roomEntity.setRentalStatus(source.getRentalStatus());
            }
        }
    }

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "price", ignore = true)
    @Mapping(target = "houseType", ignore = true)
    @Mapping(target = "rentalStatus", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    void mergeIgnorePrice(@MappingTarget RoomEntity target, BatchModifyRoomParams source);

    List<RoomVo> mapAsList(List<RoomEntity> records);

    TouristsViewRoomDetails mapToTouristsViewRoomDetails(RoomEntity findResult);

    @Mapping(target = "id", source = "roomId")
    @Mapping(target = "name", source = "roomName")
    RoomVo map(RecommendApartmentPo e);
}
